<template>
  <view>
    <div v-if="isShow" class="overlay" @click="close"></div>
    <view v-show="isShow">
      <view class="modal">
        <view class="content">
          <view class="content-title">
            <view class="content-title-img">
              <img class="imgUser" :src="userAvatar" />
            </view>
            <view class="content-title-type">
              <span class="content-name">类型：</span>
              <view>{{ modalInfo?.typeName }}</view>
            </view>
          </view>
          <view class="content-item">
            <span class="content-item-name">名称：</span>
            <view>{{ modalInfo?.name }}</view>
          </view>
          <view class="content-item">
            <span class="content-item-name">代码：</span>
            <view>{{ modalInfo?.code }}</view>
          </view>
          <view class="content-remark">
            <span class="content-remark-title">简介：</span>
            <view class="content-remark-text">{{ modalInfo?.remark }}</view>
          </view>
        </view>
        <!-- 关闭按钮 -->
        <view class="close">
          <view class="close-cancel" @click="close">取消</view>
          <view class="close-confirm" @click="confirm">申请加入</view>
        </view>
      </view>
    </view></view
  >
</template>

<script lang="ts" setup>
import { ref,watch } from 'vue'
import { schema } from '../../ts/base'
import orgCtrl from '../../ts/controller'

type Props = {
  show: boolean,
  targetId: string,
} 

watch(
  [() => props.targetId,() => props.show],
  () => {
    if(null !== props.targetId && '' !== props.targetId.trim()) {
      getScanInfo();
    }
  },
)

const props = defineProps<Props>()
const modalInfo = ref<schema.XEntity|undefined>()
const userAvatar = ref('/static/logo.png')
const emits = defineEmits(['closeScanModal','confirmScanModal'])
const isShow = ref<Boolean>(props.show ?? false);

const getScanInfo = async () => {
  const entity = await orgCtrl.user.findEntityAsync(props.targetId)
  modalInfo.value = entity
  userAvatar.value = JSON.parse(entity!.icon).thumbnail
  isShow.value=props.show;
}
const close = () => {
  isShow.value = false
  emits('closeScanModal')
}
const confirm = async () => {
  close()
  
  orgCtrl.joinTarget(props.targetId);
  emits('confirmScanModal')
}
</script>

<style lang="scss" scoped>
/* 蒙层样式 */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5); /* 半透明背景 */
  z-index: 999; /* 设置在最顶层 */
}

.modal {
  position: fixed;
  width: 80vw;
  height: 500upx;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  padding: 20upx;
  border-radius: 20upx;
  box-shadow: 0 4upx 16upx rgba(0, 0, 0, 0.15);
  z-index: 1000; /* 设置在蒙层之上 */
  .content {
    .content-title {
      width: 100%;
      height: 110upx;
      display: flex;
      align-items: center;
      .content-title-img {
        width: 50%;
        .imgUser {
          height: 100upx;
          width: 100upx;
          border-radius: 50%;
          border: 2upx solid #d3d3d3;
        }
      }
      .content-title-type {
        width: 50%;
        display: flex;
        .content-name {
          color: #8d8d8d;
        }
      }
    }
    .content-item {
      width: 100%;
      height: 60upx;
      display: flex;
      align-items: center;
      .content-item-name {
        color: #8d8d8d;
      }
    }
    .content-remark {
      display: flex;
      .content-remark-title {
        color: #8d8d8d;
      }
      .content-remark-text {
        height: 120upx;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        overflow: hidden;
        /* 控制显示行数 */
        -webkit-line-clamp: 3;
      }
    }
  }
  .close {
    position: absolute;
    bottom: 0upx;
    left: 0upx;
    width: 100%;
    height: 80upx;
    border-top: 2upx solid #d3d3d3;
    box-sizing: border-box;
    margin-top: 20upx;
    font-size: 35upx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .close-cancel {
      color: #000;
      width: 50%;
      border-right: 2upx solid #d3d3d3;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .close-confirm {
      color: #007aff;
      width: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
